package jmetal.qualityIndicator.fastHypervolume;

import java.io.IOException;

import jmetal.core.Solution;
import jmetal.core.SolutionSet;
import jmetal.qualityIndicator.util.FileRoW;

public class CalculateFastHv {
	public double calculate(SolutionSet solutionSet) {
		double hvValue = 0.0;
		int objNum = solutionSet.get(0).getNumberOfObjectives();
		Solution referencePoint = new Solution(objNum);
		for (int i = 0; i < objNum; i++) {
			referencePoint.setObjective(i, 1.1);
		}
		SolutionSet contributionSolutions = new SolutionSet(solutionSet.size());
		for (int i = 0; i < solutionSet.size(); i++) {
			Solution solution = solutionSet.get(i);
			boolean flag = true;
			for (int j = 0; j < solution.getNumberOfObjectives(); j++) {
				if (solution.getObjective(j) > referencePoint.getObjective(j)) {
					flag = false;
					break;
				}
			}
			if (flag) {
				contributionSolutions.add(solution);
			}
		}
		if (contributionSolutions.size() > 1)
			hvValue = new FastHypervolume().computeHypervolume(contributionSolutions, referencePoint);
		return hvValue;
	}

	public static void main(String[] args) throws IOException {
		int objNum = 6;
		int size = 800;
		

		Solution referencePoint = new Solution(objNum);
		for (int i = 0; i < objNum; i++) {
			referencePoint.setObjective(i, -20000);
		}
		String solutionFrontPath = "input_data/temp";
		SolutionSet solutionSet = new FileRoW().getPopulationFromFile(solutionFrontPath, size);
		SolutionSet contributionSolutions = new SolutionSet(solutionSet.size());
		for (int i = 0; i < solutionSet.size(); i++) {
		Solution solution = solutionSet.get(i);
		boolean flag = true;
		for (int j = 0; j < solution.getNumberOfObjectives(); j++) {
			if (solution.getObjective(j) > referencePoint.getObjective(j)) {
				flag = false;
				break;
			}
		}
		if (flag) {
			contributionSolutions.add(solution);
		}
	}
	double hvValue = new FastHypervolume().computeHypervolume(contributionSolutions, referencePoint);
	System.out.println(hvValue);
		
		
//		for (int probNum = 0; probNum < 9; probNum++) {
//			String problem = "WFG" + (probNum + 1);
//			double[] hvValues = new double[20];
//			
//			String hv_valuesPath = "results/hv_results/" + algorithm + "_" + problem + "_" + objNum + "D.dat";
//			System.out.println(hv_valuesPath);
//			
//			FileOutputStream output = new FileOutputStream(
//					new File(hv_valuesPath));
//			for (int k = 0; k < 20; k++) {
//				String solutionFrontPath = root + "/" + algorithm + "/" + problem + "/" + objNum + "D/FUN" + k;
//				SolutionSet solutionSet = new FileRoW().getPopulationFromFile(solutionFrontPath, size);
//				SolutionSet contributionSolutions = new SolutionSet(solutionSet.size());
//				for (int i = 0; i < solutionSet.size(); i++) {
//					Solution solution = solutionSet.get(i);
//					boolean flag = true;
//					for (int j = 0; j < solution.getNumberOfObjectives(); j++) {
//						if (solution.getObjective(j) > referencePoint.getObjective(j)) {
//							flag = false;
//							break;
//						}
//					}
//					if (flag) {
//						contributionSolutions.add(solution);
//					}
//				}
//				double hvValue = new FastHypervolume().computeHypervolume(contributionSolutions, referencePoint);
//				output.write(("Run" + k + ":\t" + hvValue + "\n").getBytes());
//				hvValues[k] = hvValue;
//				System.out.println(hvValue);
//
//			}
//			double ave = MathUtil.getAvarage(hvValues);
//			output.write(("ave:\t" + ave + "\n").getBytes());
//			double std = MathUtil.getStd(hvValues);
//			output.write(("std:\t" + std + "\n").getBytes());
//			output.close();
//		}
	}
}
